<template>
  <Dialog
    v-model:visible="dialog.visible"
    modal
    header="批量编辑用户信息"
    @show="openHandler"
    @hide="closeHandler"
  >
    <el-form ref="dataForm" :model="entity">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            prop="userPassword"
            label="用户密码"
            :rules="{
              pattern: regular.password,
              message: regular.passwordMsg,
              trigger: 'blur',
            }"
          >
            <InputGroup>
              <InputText
                v-model="entity.userPassword"
                placeholder="请输入用户密码"
              />
              <Button
                :icon="passwordLoading ? '' : 'pi pi-refresh'"
                :loading="passwordLoading"
                @click="getRandomPasswordHandler"
              />
            </InputGroup>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="roleIds" label="用户角色">
            <MultiSelect
              v-model="entity.roleIds"
              :options="roles"
              dataKey="id"
              optionValue="id"
              optionLabel="roleName"
              placeholder="请选择角色"
              display="chip"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="userDeptId" label="用户部门">
            <dept-tree-select v-model="entity.userDeptId" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="userEnable" label="用户是否启用">
            <ToggleSwitch v-model="entity.userEnable" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <cancel-button @click="dialog.visible = false" />
      <save-button :loading="loading" @click="saveHandler" />
    </template>
  </Dialog>
</template>
<script>
import { batchUpdate } from '@/api/upms/user'
import { fetchRoleList } from '@/api/personal'
import { randomCode } from '@/api/personal/common'
import { regular } from '@/utils/formRegular'

export default {
  name: 'UserBatchEdit',
  props: {
    dialog: {
      type: Object,
      required: true,
    },
  },
  emits: ['success'],
  data() {
    return {
      loading: false,
      entity: {},
      roles: [],
      passwordLoading: false,
    }
  },
  computed: {
    regular() {
      return regular
    },
  },
  mounted() {
    fetchRoleList().then((res) => {
      this.roles = res.data.data
    })
  },
  methods: {
    openHandler() {
      this.entity = {
        userIds: this.dialog.userIds,
        userPassword: '',
        roleIds: [],
        userDeptId: null,
        userEnable: true,
      }
    },
    closeHandler() {
      this.entity = {}
    },
    saveHandler() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          batchUpdate(this.entity)
            .then(() => {
              this.dialog.visible = false
              this.$message.success('保存成功')
              this.$emit('success')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    getRandomPasswordHandler() {
      this.passwordLoading = true
      randomCode({
        upperCaseLetterLength: 2,
        lowerCaseLetterLength: 2,
        numLength: 4,
      }).then((res) => {
        this.passwordLoading = false
        this.entity.userPassword = res.data.data
      })
    },
  },
}
</script>
